---
--- Created by HUAWEI.
--- DateTime: 2025/9/20 15:00
---秒杀业务资格判断：

---参数列表
local voucherId = ARGV[1]
local userId = ARGV[2]
local orderId=ARGV[3]

---key
local stockKey = 'seckill:stock:' .. voucherId
local orderKey = 'seckill:order:' .. voucherId

---判断库存是否充足
local stock = tonumber(redis.call("get", stockKey))
if stock <= 0 then
    return 1
end

---判断用户是否已经下过单
local isOrder = redis.call("sismember", orderKey, userId)
if isOrder==1 then
    return 2
end

---扣减库存
redis.call("incrby", stockKey,-1)

---下订单
redis.call("sadd", orderKey, userId)
---发送消息到队列                              为了转换实体类方便，直接叫做id
redis.call("xadd", "stream.orders", "*", "voucherId", voucherId, "userId", userId, "id", orderId)
return 0
